import 'dart:math';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test_1/just_test_text.dart';

class CustomScrollViewDemo extends StatefulWidget {
  const CustomScrollViewDemo({Key? key}) : super(key: key);

  @override
  _CustomScrollViewDemoState createState() => _CustomScrollViewDemoState();
}

class _CustomScrollViewDemoState extends State<CustomScrollViewDemo> with SingleTickerProviderStateMixin {
  late TabController tabController;

  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text("标题")),
        body: EasyRefresh(
          onRefresh: () {
            return Future.delayed(const Duration(seconds: 2), () {
              // 刷新成功
            });
          },
          onLoad: () {
            return Future.delayed(const Duration(seconds: 2), () {
              // 加载成功
            });
          },
          child: CustomScrollView(
            slivers: <Widget>[
              _buildBanner(),
              _buildStickyBar(),
              _buildList(),
            ],
          ),
        ));
  }

  Widget _buildBanner() {
    return SliverToBoxAdapter(
      child: Container(
        width: double.infinity,
        height: 200,
        child: Image.asset(
          "assets/images/btn_select_gende_male_selected.png",
          fit: BoxFit.fill,
        ),
      ),
    );
  }

  Widget _buildStickyBar() {
    return SliverPersistentHeader(
      pinned: true, //是否固定在顶部
      floating: true,
      delegate: _SliverAppBarDelegate(
          minHeight: 50, //收起的高度
          maxHeight: 50, //展开的最大高度
          child: Container(
            padding: const EdgeInsets.only(left: 16),
            color: Colors.pink,
            alignment: Alignment.centerLeft,
            child: TabBar(
              controller: tabController,
              tabs: const [
                Tab(
                  text: "热门",
                ),
                Tab(
                  text: "推荐",
                )
              ],
            ),
          )),
    );
  }

  Widget _buildList() {
    return SliverFillRemaining(
      child: TabBarView(
        controller: tabController,
        children: [
          _buildTabBarViewList(0),
          _buildTabBarViewList(1),
        ],
      ),
    );
  }

  Widget _buildTabBarViewList(int page) {
    return ListView.builder(
      itemCount: 100,
      itemBuilder: (BuildContext context, int index) {
        return JustTestText(
          "测试$page -- $index",
        );
      },
    );
  }
}

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SliverAppBarDelegate({
    required this.minHeight,
    required this.maxHeight,
    required this.child,
  });

  final double minHeight;
  final double maxHeight;
  final Widget child;

  @override
  double get minExtent => minHeight;

  @override
  double get maxExtent => max(maxHeight, minHeight);

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return new SizedBox.expand(child: child);
  }

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight || minHeight != oldDelegate.minHeight || child != oldDelegate.child;
  }
}
